﻿#ifndef CUSTOMITEMMODEL_H
#define CUSTOMITEMMODEL_H

#include <QStandardItemModel>

class CustomItemModel : public QStandardItemModel
{
    Q_OBJECT
public:
    CustomItemModel(QObject *parent = nullptr) : QStandardItemModel(parent) {}

    // 重写setData方法以发射dataChanged信号
    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override {
        bool result = QStandardItemModel::setData(index, value, role);
        if (result) {
            emit dataChanged(index,  value,QVector<int>() << role);
        }
        return result;
    }
    Qt::ItemFlags flags(const QModelIndex &index) const override
    {
        Qt::ItemFlags defaultFlags = QStandardItemModel::flags(index);
        // 检查当前项是否是根节点的直接子节点（一级节点 禁用编辑）
        if (!index.parent().isValid())
        {
            return defaultFlags & ~Qt::ItemIsEditable;

        }

        return defaultFlags;
    }
signals:
    void dataChanged(const QModelIndex &topLeft, const QVariant& var,const QVector<int> &roles = QVector<int>());
};

#endif // CUSTOMITEMMODEL_H
